//
//  StatusCell.swift
//  weibo19
//
//  Created by Arvin on 15/11/14.
//  Copyright © 2015年 Arvin. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellIconViewWidth = 35
let StatusCellMargin = 12

class StatusCell: UITableViewCell {
    private var bottomConstraint: Constraint?
    
    var status: Status? {
        didSet {
            originalView.status = status
            let retweeted = status?.retweeted_status
            bottomConstraint?.uninstall()
            if retweeted == nil {
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.top.equalTo(originalView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                // 给转发微博传模型数据
                retweetedView.retweetedStatus = status?.retweeted_status
                retweetedView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
        }
        
        // 添加转发微博
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            bottomConstraint = make.top.equalTo(retweetedView.snp_bottom).offset(StatusCellMargin).constraint
            make.left.equalTo(retweetedView.snp_left)
            make.right.equalTo(retweetedView.snp_right)
            make.height.equalTo(StatusCellIconViewWidth)
        }
        
        // 注意: contentView也得添加约束 , 自上而下的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.equalTo(snp_top)
            make.left.equalTo(snp_left)
            make.right.equalTo(snp_right)
        }
    }
    
    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    private lazy var retweetedView: StatusRetweetedView = StatusRetweetedView()
    
}
